package com.mybatis.controller;


import com.mybatis.entity.TUser;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * 相当于Handller
 * 实现controller接口的 处理器
 * Created by 85243 on 2017/5/19.
 */


/**整个流程
 * DEBUG [http-nio-8080-exec-3] - DispatcherServlet with name 'springmvc' processing GET request for [/ssm/queryUser.action]
 DEBUG [http-nio-8080-exec-3] - Mapping [/queryUser.action] to HandlerExecutionChain with handler [com.mybatis.controller.UserController@1796ade2] and 1 interceptor
 DEBUG [http-nio-8080-exec-3] - Last-Modified value for [/ssm/queryUser.action] is: -1
 DEBUG [http-nio-8080-exec-3] - Rendering view [org.springframework.web.servlet.view.JstlView: name '/jsp/user/userList.jsp'; URL [/jsp/user/userList.jsp]] in DispatcherServlet with name 'springmvc'
 DEBUG [http-nio-8080-exec-3] - Added model object 'userList' of type [java.util.ArrayList] to request in view with name '/jsp/user/userList.jsp'
 DEBUG [http-nio-8080-exec-3] - Forwarding to resource [/jsp/user/userList.jsp] in InternalResourceView '/jsp/user/userList.jsp'
 DEBUG [http-nio-8080-exec-3] - Successfully completed request
 */
public class UserController implements Controller {

    @Override
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {

        List<TUser> userList = new ArrayList<>();

        TUser user1= new TUser();
        user1.setName("傻狗");
        user1.setId(1);
        userList.add(user1);
        TUser user2= new TUser();
        user2.setName("猪脑子");
        user2.setId(2);
        userList.add(user2);
        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //相当于request里面的setAttribute,在jsp页面通过itemList取数据
        modelAndView.addObject("userList",userList);
        //指定视图
        modelAndView.setViewName("/jsp/user/userList.jsp");
        return modelAndView;
    }
}
